﻿// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证（版本 2.0）进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动！任何基于本项目二次开发而产生的一切法律纠纷和责任，我们不承担任何责任！

using Admin.NET.Core.Service;
using Microsoft.AspNetCore.Http;
namespace Admin.NET.Application;

/// <summary>
/// 班组服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class BanZuService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<BanZu> _banZuRep;
    public BanZuService(SqlSugarRepository<BanZu> banZuRep)
    {
        _banZuRep = banZuRep;
    }

    /// <summary>
    /// 分页查询班组
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    [DisplayName("分页查询班组")]
    public async Task<SqlSugarPagedList<BanZuOutput>> Page(PageBanZuInput input)
    {
		input.SearchKey = input.SearchKey?.Trim();
        var query = _banZuRep.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(input.SearchKey), u =>
                u.CompanyNumber.Contains(input.SearchKey)
                || u.ProjectNumber.Contains(input.SearchKey)
                || u.Token.Contains(input.SearchKey)
                || u.TeamName.Contains(input.SearchKey)
                || u.ProjectPart.Contains(input.SearchKey)
                || u.Name.Contains(input.SearchKey)
                || u.Tel.Contains(input.SearchKey)
                || u.IDNum.Contains(input.SearchKey)
                || u.EmerPeople.Contains(input.SearchKey)
                || u.EmerTel.Contains(input.SearchKey)
                || u.ContractID.Contains(input.SearchKey)
            )
            .WhereIF(!string.IsNullOrWhiteSpace(input.CompanyNumber), u => u.CompanyNumber.Contains(input.CompanyNumber.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.ProjectNumber), u => u.ProjectNumber.Contains(input.ProjectNumber.Trim()))
            .WhereIF(input.ProjectId>0, u => u.ProjectId == input.ProjectId)
            .WhereIF(!string.IsNullOrWhiteSpace(input.Token), u => u.Token.Contains(input.Token.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.TeamName), u => u.TeamName.Contains(input.TeamName.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.ProjectPart), u => u.ProjectPart.Contains(input.ProjectPart.Trim()))
            .WhereIF(input.ContractSum>0, u => u.ContractSum == input.ContractSum)
            .WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Tel), u => u.Tel.Contains(input.Tel.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.IDNum), u => u.IDNum.Contains(input.IDNum.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.EmerPeople), u => u.EmerPeople.Contains(input.EmerPeople.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.EmerTel), u => u.EmerTel.Contains(input.EmerTel.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.ContractID), u => u.ContractID.Contains(input.ContractID.Trim()))
            .Select<BanZuOutput>();
		return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加班组
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    [DisplayName("增加班组")]
    public async Task<long> Add(AddBanZuInput input)
    {
        var entity = input.Adapt<BanZu>();
        await _banZuRep.InsertAsync(entity);
        return entity.Id;
    }

    /// <summary>
    /// 删除班组
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    [DisplayName("删除班组")]
    public async Task Delete(DeleteBanZuInput input)
    {
        var entity = await _banZuRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _banZuRep.FakeDeleteAsync(entity);   //假删除
        //await _banZuRep.DeleteAsync(entity);   //真删除
    }

    /// <summary>
    /// 更新班组
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    [DisplayName("更新班组")]
    public async Task Update(UpdateBanZuInput input)
    {
        var entity = input.Adapt<BanZu>();
        await _banZuRep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取班组
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    [DisplayName("获取班组")]
    public async Task<BanZu> Detail([FromQuery] QueryByIdBanZuInput input)
    {
        return await _banZuRep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取班组列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    [DisplayName("获取班组列表")]
    public async Task<List<BanZuOutput>> List([FromQuery] PageBanZuInput input)
    {
        return await _banZuRep.AsQueryable().Select<BanZuOutput>().ToListAsync();
    }





}
